/*
* @Author: asdw
* @Date:   2017-12-25 14:49:22
* @Last Modified by:   asdw
* @Last Modified time: 2017-12-25 14:54:57
*/

/*event,isPC*/
var Yeffect = {
    event: {
        start: function (dom, fn) {
            dom = $(dom).eq(0);
            if (!this.move.touchs) {
                this.move.touchs = {}
            };
            if (!this.start.ISpc) {
                this.start.ISpc = Yeffect.isPC()
            };
            var thiso = this;
            if (this.start.ISpc) {
                thiso.move.touchs = null;
                dom.mousedown(function (e) {
                    thiso.move.touchs = e;
                    fn.call(this, e)
                })
            } else {
                dom[0].addEventListener("touchstart", function (e) {
                    var touchs = e.touches;
                    var thiskey = Object.keys(touchs);
                    var minlen = thiskey.length - 2;
                    minlen = Math.max(minlen, 0);
                    var thistouch = touchs[thiskey[minlen]];
                    for (var o in touchs) {
                        if (!thiso.move.touchs[thistouch.identifier]) {
                            var datsA = {};
                            var datsM = {};
                            for (var p in thistouch) {
                                datsA[p] = thistouch[p];
                                datsM[p] = thistouch[p];
                            };
                            var newdtas = {
                                dataS: datsA,
                                dataM: datsM
                            };
                            thiso.move.touchs[thistouch.identifier] = JSON.parse(JSON.stringify(newdtas))
                        }
                    };
                    thistouch.preventDefault = function () {
                        e.preventDefault()
                    };
                    fn.bind(this)(thistouch, touchs)
                })
            }
        },
        move: function (dom, fn) {
            if (!this.move.touchs) {
                this.start(dom, function () {})
            };
            dom = $(dom).eq(0);
            var thiso = this;
            if (this.start.ISpc) {
                dom.mousemove(function (e) {
                    if (thiso.move.touchs) {
                        e.mousedown = true;
                        e.deltaX = e.pageX - thiso.move.touchs.pageX;
                        e.deltaY = e.pageY - thiso.move.touchs.pageY;
                    } else {
                        e.deltaX = 0;
                        e.deltaY = 0;
                        e.mousedown = false;
                    };
                    fn.call(this, e)
                })
            } else {
                dom[0].addEventListener("touchmove", function (e) {
                    var touchs = e.touches;
                    for (var o in touchs) {
                        var idter = touchs[o].identifier;
                        if (idter != undefined) {
                            if (touchs[o].pageX != thiso.move.touchs[idter].dataM.pageX || touchs[o].pageY != thiso.move
                                .touchs[idter].dataM.pageY) {
                                thiso.move.touchs[idter].dataM.pageX = touchs[o].pageX;
                                thiso.move.touchs[idter].dataM.pageY = touchs[o].pageY;
                                var thistouch = touchs[o];
                                thistouch.preventDefault = function () {
                                    e.preventDefault()
                                };
                                thistouch.deltaX = thiso.move.touchs[idter].dataM.pageX - thiso.move.touchs[idter].dataS
                                    .pageX;
                                thistouch.deltaY = thiso.move.touchs[idter].dataM.pageY - thiso.move.touchs[idter].dataS
                                    .pageY;
                                thiso.move.touchs[idter].deltaX = thistouch.deltaX;
                                thiso.move.touchs[idter].deltaY = thistouch.deltaY;
                                fn.bind(this)(thistouch, touchs)
                            }
                        };
                    }
                })
            }
        },
        end: function (dom, fn) {
            if (!this.move.touchs) {
                this.start(dom, function () {})
            };
            dom = $(dom).eq(0);
            var thiso = this;
            if (this.start.ISpc) {
                dom.mouseup(function (e) {
                    if (thiso.move.touchs) {
                        e.deltaX = e.pageX - thiso.move.touchs.pageX;
                        e.deltaY = e.pageY - thiso.move.touchs.pageY;
                    } else {
                        e.deltaX = 0;
                        e.deltaY = 0;
                    };
                    e.mousedown = false;
                    thiso.move.touchs = null;
                    fn.call(dom[0], e)
                })
            } else {
                dom[0].addEventListener("touchend", function (e) {
                    var touchs = e.touches;
                    var curkey = [];
                    for (var o in touchs) {
                        if (touchs[o].identifier != undefined) {
                            curkey.push(touchs[o].identifier + "")
                        }
                    };
                    for (var t in thiso.move.touchs) {
                        if (curkey.indexOf(t) < 0) {
                            var touchtT = thiso.move.touchs[t];
                            if (!touchtT.deltaY && touchtT.deltaY != 0) {
                                touchtT.deltaX = 0;
                                touchtT.deltaY = 0;
                            };
                            fn.bind(this)(touchtT);
                            delete thiso.move.touchs[t];
                        }
                    };
                })
            }
        }
    },
    isPC: function () {
        var userAgentInfo = navigator.userAgent;
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
        var flag = true;
        for (var v = 0; v < Agents.length; v++) {
            if (userAgentInfo.indexOf(Agents[v]) > -1) {
                flag = false;
                break
            }
        }
        return flag
    }
};
